<template>
  <div class="container">
    <div class="app-container">
      <!-- 角色管理内容 -->
      <div class="role-operate">
        <el-button size="mini" type="primary" @click="showDialog = true">添加角色</el-button>

      </div>

      <!-- 放置table组件 -->
      <el-table :data="list">
        <!-- 放置列 -->
        <el-table-column prop="name" align="center" width="200" label="序号">
          <template v-slot="{ $index }">
            {{
              $index + 1 + pageParams.pagesize * (pageParams.page - 1) }}
          </template>
        </el-table-column>
        <el-table-column prop="name" align="center" width="200" label="角色">
          <!-- 3. 作用域插槽+条件渲染 -->
          <template v-slot="{ row }">
            <div v-if="!row.isEdit">{{ row.name }}</div>
            <!-- 3. 使用复制的数据 双向绑定 -->
            <el-input v-else v-model="row.editRowData.name" />
          </template>
        </el-table-column>
        <el-table-column prop="state" align="center" width="200" label="启用">
          <template v-slot="{ row }">
            <div v-if="!row.isEdit">{{ row.state ? '已启用' : '未启用' }}</div>
            <!-- 3. 使用复制的数据 双向绑定 -->
            <el-switch v-else v-model="row.editRowData.state" :active-value="1" :inactive-value="0" />
          </template>
        </el-table-column>
        <el-table-column prop="description" align="center" label="描述">
          <template v-slot="{ row }">
            <div v-if="!row.isEdit">{{ row.description }}</div>
            <el-input v-else v-model="row.editRowData.description" type="textarea" />
          </template>

        </el-table-column>
        <el-table-column align="center" label="操作">        <!-- 放置操作按钮 -->
          <template v-slot="{ row }">
            <div v-if="!row.isEdit">
              <el-button size="mini" type="text">分配权限</el-button>

              <el-button size="mini" type="text" @click="row.isEdit = true">编辑</el-button>
              <el-popconfirm title="这是一段内容确定删除吗？" @onConfirm="confirmDel(row.id)">
                <el-button slot="reference" style="margin-left:10px" size="mini" type="text">删除</el-button>

              </el-popconfirm>
            </div>
            <div v-else>
              <el-button size="mini" type="primary" @click="onUpdate(row)">确定</el-button>
              <el-button size="mini" @click="btnCancel(row)">取消</el-button>
            </div>

          </template></el-table-column>
      </el-table>

      <!-- 放置分页组件 -->
      <el-row type="flex" style="height: 60px" align="middle" justify="center">
        <!-- 放置分页组件 -->
        <el-pagination
          :page-size="pageParams.pagesize"
          :current-page="pageParams.page"
          :total="pageParams.total"
          layout="prev, pager, next"
          @current-change="changePage"
        />
      </el-row>
      <el-dialog width="500px" title="新增角色" :visible.sync="showDialog">
        <!-- 表单内容 -->
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="120px">
          <el-form-item prop="name" label="角色名称">
            <el-input v-model="roleForm.name" style="width:300px" size="mini" />
          </el-form-item>

          <el-form-item label="启用">
            <!-- 如果不需要校验 就不需要写 prop属性 -->
            <el-switch v-model="roleForm.state" :active-value="1" :inactive-value="0" size="mini" />
          </el-form-item>

          <el-form-item prop="description" label="角色描述">
            <el-input v-model="roleForm.description" type="textarea" :rows="3" style="width:300px" size="mini" />
          </el-form-item>
          <el-form-item>
            <el-row type="flex" justify="center">
              <el-col :span="12">
                <el-button type="primary" size="mini" @click="btnOK">确定</el-button>

                <el-button size="mini" @click="onCancel">取消</el-button>

              </el-col>

            </el-row>

          </el-form-item>

        </el-form>

      </el-dialog>

    </div>
  </div>
</template>
<script>
import { getRoleList, addRole, updateRoleAPI, delRole } from '@/api/role'
export default {
  name: 'Role',
  data() {
    return {
      showDialog: false,
      list: [],
      pageParams: {
        page: 1, // 第几页
        pagesize: 5, // 每页多少条
        total: 0
      },
      roleForm: {
        name: '',
        description: '',
        state: 0 // 默认未1启用 关闭 0 打开1
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '角色描述不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    async getRoleList() {
      const { rows, total } = await getRoleList(this.pageParams)
      this.list = rows // 赋值数据
      this.list.forEach((item) => {
        // item.isEdit = false
        // 1. 给list中每个元素, 分配一个isEdit 表示是否正在编辑的 开关表示
        this.$set(item, 'isEdit', false)
        // 2. 复制出来一份数据
        this.$set(item, 'editRowData', {
          name: item.name,
          state: item.state,
          description: item.description
        })
      })
      this.pageParams.total = total
    },
    changePage(newPage) {
      this.pageParams.page = newPage // 赋值当前页码
      this.getRoleList()
    },
    btnOK() {
      this.$refs.roleForm.validate(async isOK => {
        if (isOK) {
          await addRole(this.roleForm)
          this.$message.success('新增角色成功')
          this.getRoleList()
          this.onCancel()
        }
      })
    },
    btnCancel(row) {
      // 4. 还原数据并关闭编辑状态
      row.editRowData = {
        name: row.name,
        state: row.state,
        description: row.description
      }
      row.isEdit = false
    },
    // 取消
    onCancel() {
      this.$refs.roleForm.resetFields()

      this.showDialog = false
    },
    async onUpdate(row) {
      // 2. 校验数据不为空
      if (!row.editRowData.name) return this.$message.error('角色名称不能为空')
      if (!row.editRowData.description) return this.$message.error('角色描述不能为空')

      // 3. 调用API
      await updateRoleAPI({ id: row.id, ...row.editRowData })
      // 4. 提示用户和刷新数据
      this.$message.success('修改成功')
      this.getRoleList()
    },
    async  confirmDel(id) {
      await delRole(id) // 后端删除
      this.$message.success('删除角色成功')
      // 删除的如果是最后一个
      if (this.list.length === 1) this.pageParams.page--
      this.getRoleList()
    }
  }
}
</script>
<style scoped>
.role-operate {
  padding: 10px;
}
</style>
